import { AxiosResponse } from 'axios'
import { api } from '../boot/axios'
import { DetailFormType, Parts } from './type'

function url(s: string) {
  return ['/api/parts', s.replace('/', ''), ''].join('/')
}


export function searchPart(stock_code: string, serial_no: string) {
  return api({
    url: url('get_part'),
    method: 'post',
    data: {
      stock_code:stock_code.toUpperCase(),
      serial_no
    }
  })
}

export function getParts() {
  return api({
    url: '/api/parts/',
    method: 'get',
  })
}

export function createQrCode(detailForm:DetailFormType) {
  console.log(detailForm);
  
  return api({
    url: url('create_part'),
    method: 'post',
    data: {
      'stock_code':detailForm.stock_code.toUpperCase(),
      'serial_no':detailForm.serial_no.toUpperCase(),
      'car': detailForm.car,
      'workgroup': detailForm.workgroup,
      'system':detailForm.system,
      'short_description':detailForm.description,
      'description':detailForm.detail_description,
      'date_code':detailForm.date_code,
      'sw_ver':detailForm.sw_ver,
      'hw_ver':detailForm.hw_ver,
      'asset_id':detailForm.asset_id,
      'obsolescence':detailForm.obsolescence,
      'equipment_class':detailForm.equipmentClass,
      'lv1':detailForm.lv1,
      'lv2':detailForm.lv2,
      'lv3':detailForm.lv3,
      'lv4':detailForm.lv4,
      'lv5':detailForm.lv5, 
      'parent_part_stock_code':detailForm.parent_part_stock_code ,
      'parent_part_serial_number':detailForm.parent_part_serial_number,
      'stock_item_regular_cycle_month': Number(detailForm.stock_item_regular_cycle_month),
    }
  })
}

export function editQrCode(id:number ,detailForm:DetailFormType) {
  console.log(detailForm);
  
  return api({
    url: url(`${id}`),
    method: 'patch',
    data: {
      'stock_code':detailForm.stock_code.toUpperCase(),
      'serial_no':detailForm.serial_no,
      'system':detailForm.system,
      'car': detailForm.car,
      'workgroup': detailForm.workgroup,
      'description':detailForm.description,
      'detail_description':detailForm.detail_description,
      'date_code':detailForm.date_code,
      'sw_ver':detailForm.sw_ver,
      'hw_ver':detailForm.hw_ver,
      'asset_id':detailForm.asset_id,
      'obsolescence':detailForm.obsolescence,
      'equipment_class':detailForm.equipmentClass,
      'lv1':detailForm.lv1,
      'lv2':detailForm.lv2,
      'lv3':detailForm.lv3,
      'lv4':detailForm.lv4,
      'lv5':detailForm.lv5, 
      'parent_part_stock_code':detailForm.parent_part_stock_code ,
      'parent_part_serial_number':detailForm.parent_part_serial_number,
      'stock_item_regular_cycle_month': Number(detailForm.stock_item_regular_cycle_month),
    }
  })
}

export function importDevices(stockCode: string, serialNo: string) {
  return api({
    url: url('search'),
    method: 'post',
    data: {
      stockCode,
      serialNo
    }
  })
}

export function getPartDetailData(part_id:number):Promise<AxiosResponse<Parts>>{
  return api({
    url:url(`${part_id}`),
    method: 'get',
  })
}

export function getChildPartsList(stock_code: string, serial_no: string){
  return api({
    url:url('get_child_parts'),
    method: 'post',
    data:{
      parent_stock_code:stock_code.toUpperCase(),
      parent_serial_no: serial_no
    }
  })
}

export function getPartHistoryData(stock_code:string, serial_no:string){
  return api({
    url: url('get_check_in_out_history'),
    method:'post',
    data: {
      stock_code:stock_code.toUpperCase(),
      serial_number: serial_no
    }
  })
}

export function assignChildParts(stock_code:string, serial_no:string,
  parent_part_stock_code:string, parent_part_serial_no:string, sequence:number){
  return api({
    url: url('assign_part_child'),
    method:'post',
    data: {
      parent_part_stock_code:parent_part_stock_code.toUpperCase(),
      parent_part_serial_no,
      stock_code:stock_code.toUpperCase(),
      serial_no,
      sequence
    }
  })
}

export function removeChildParts(stock_code:string, serial_no:string,
  parent_part_stock_code:string, parent_part_serial_no:string){
  return api({
    url: url('remove_part_child'),
    method:'post',
    data: {
      parent_part_stock_code:parent_part_stock_code.toUpperCase(),
      parent_part_serial_no,
      stock_code:stock_code.toUpperCase(),
      serial_no
    }
  })
}

export function getPartsVersion(stock_code: string, serial_no:string){
  return api({
    url: url('get_verison'),
    method:'post',
    data: {
      stock_code,
      serial_number:serial_no
    }
  })
}

export function getPartsOrderHistory(serial_numbers:string[]){
  return api({
    url: url('get_maintenance_history'),
    method: 'post',
    data: {
      serial_numbers
    }
  })
}

export function getPartHardTimeReplacement(stock_code:string,serial_no:string){
  return api({
    url: url('get_hard_time_replacement'),
    method: 'post',
    data: {
      stock_code:stock_code.toUpperCase(),
      serial_no,
    }
  })
}

export function getCheckStatus(stock_code:string,serial_no:string){
  return api({
    url: url('check_status'),
    method: 'post',
    data: {
      stock_code:stock_code.toUpperCase(),
      serial_no,
    }
  })
}

export function fetchPartWithNumOfChild(){
  return api({
    url: url('get_parts_with_num_of_child'),
    method: 'get',
  })
}

export function fetchPartWithNumOfChildFillter(stock_code:string, serial_no:string, description:string){
  return api({
    url: url('get_parts_with_num_of_child_fillter'),
    method: 'post',
    data: {
      stock_code: stock_code,
      serial_no: serial_no,
      description: description
    }
  })
}

export function softDeletePart(id:number){
  return api({
    url: url('soft_delete_part'),
    method: 'delete',
    data:{
      id
    }
  })
}


export function getQrcode(part_id:number, qrcode_size: number, cews_size: number){
  return api({
    url: url('get_qr_code_image'),
    method: 'post',
    data:{
      part_id,
      qrcode_size,
      cews_size
    }
  })
}

export function getChildPartsListimages(parent_part_id:number, qrcode_size: number, cews_size: number){
  return api({
    url: url('get_childs_qr_code_image'),
    method: 'post',
    data:{
      parent_part_id,
      qrcode_size,
      cews_size
    }
  })
}
